package com.bensonbasement.snorflux.server.verbs;

import com.bensonbasement.snorflux.commands.DirectionAPFormula;
import com.bensonbasement.snorflux.commands.IAPFormula;
import com.bensonbasement.snorflux.commands.TargetMap;
import com.bensonbasement.snorflux.commands.TargetRequest;
import com.bensonbasement.snorflux.server.events.WalkEvent;
import com.bensonbasement.snorflux.server.world.Actor;
import com.bensonbasement.snorflux.server.world.Entity;
import com.bensonbasement.snorflux.server.world.Space;
import com.bensonbasement.snorflux.utils.Direction;

public class WalkVerb extends Verb {
	public static final WalkVerb WALK;
	static {
		TargetRequest[] requests = {TargetRequest.DIRECTIONREQUEST};
		WALK = new WalkVerb("move",null,requests);
	}
	
	private static final IAPFormula APFORMULA = new DirectionAPFormula(1);
	
	private WalkVerb(String verbName, String iconGlyph,
			TargetRequest[] targetRequests) {
		super(verbName, iconGlyph, targetRequests);
	}
	
	@Override
	protected void actuallyExecute(Actor actor, Entity object, TargetMap targets) throws VerbExecuteCanceledException {
		Direction dir = targets.getDirectionTarget(TargetRequest.DIRECTIONREQUEST.getTargetName());
		Space startSpace = actor.getSpace();
		Space newSpace = startSpace.getNeighbor(dir);
		if (newSpace == null || newSpace.getBigEntity() != null) {
			throw new VerbExecuteCanceledException();
		}
		else {
			WalkEvent event = new WalkEvent(actor, startSpace, dir);
			actor.getWorld().fireEvent(event);
			actor.setLocation(newSpace);
		}
	}
	
	@Override
	public boolean canActorPerform(Actor actor) {
		return true;
	}
	
	@Override
	public boolean checkRelevant(Actor actor, Entity object) {
		return true;
	}
	
	@Override
	public boolean checkEnabled(Actor actor, Entity object) {
		return true;
	}
	
	@Override
	public IAPFormula getAPFormula(Actor actor, Entity object) {
		return APFORMULA;
	}
}
